﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Beeldbankmvc.Models
{
    public class RegioFacetRepository
    {
        BeeldbankLinqDataContext db = new BeeldbankLinqDataContext();

        public IEnumerable<FacetModel> getAllRegios()
        {
            return from o in FindAllRegios()
                   orderby o.Regio
                   select new FacetModel { 
                       Id = o.Id,
                       Description = o.Regio, 
                   };
        }

        private IQueryable<BEELDBANK_MEDIAOBJECT_FACETS_REGIO> FindAllRegios()
        {
            return db.BEELDBANK_MEDIAOBJECT_FACETS_REGIOs;
        }
    }

    public class TechniekFacetRepository
    {
        BeeldbankLinqDataContext db = new BeeldbankLinqDataContext();

        public IEnumerable<FacetModel> getAllTechniek()
        {
            return from o in FindAllTechniek()
                   orderby o.Techniek
                   select new FacetModel
                   {
                       Id = o.Id,
                       Description = o.Techniek,
                   };
        }

        private IQueryable<BEELDBANK_MEDIAOBJECT_FACETS_TECHNIEK> FindAllTechniek()
        {
            return db.BEELDBANK_MEDIAOBJECT_FACETS_TECHNIEKs;
        }
    }

    public class RiviergebiedFacetRepository
    {
        BeeldbankLinqDataContext db = new BeeldbankLinqDataContext();

        public IEnumerable<FacetModel> getAllRiviergebied()
        {
            return from o in FindAllRiviergebied()
                   orderby o.Riviergebied
                   select new FacetModel
                   {
                       Id = o.Id,
                       Description = o.Riviergebied,
                   };
        }

        private IQueryable<BEELDBANK_MEDIAOBJECT_FACETS_RIVIERGEBIED> FindAllRiviergebied()
        {
            return db.BEELDBANK_MEDIAOBJECT_FACETS_RIVIERGEBIEDs;
        }
    }

    public class ProjectFacetRepository
    {
        BeeldbankLinqDataContext db = new BeeldbankLinqDataContext();

        public IEnumerable<FacetModel> getAllProject(string publication)
        {
            return from o in FindAllProject()
                   where o.Publicatie.Equals(publication)
                   orderby o.Project
                   select new FacetModel
                   {
                       Id = o.Id,
                       Description = o.Project,
                   };
        }

        private IQueryable<BEELDBANK_MEDIAOBJECT_FACETS_PROJECT> FindAllProject()
        {
            return db.BEELDBANK_MEDIAOBJECT_FACETS_PROJECTs;
        }
    }

    public class YearRepository
    {
        MediaObjectRepository mo = new MediaObjectRepository();

        public List<BEELDBANK_MEDIAOBJECT_FACETS_RESULT> getDistinctYears()
        {
            List<BEELDBANK_MEDIAOBJECT_FACETS_RESULT> yearList = new List<BEELDBANK_MEDIAOBJECT_FACETS_RESULT>();
            
            foreach (var year in (from p in mo.FindAll() select p.Datum_Realisatie.Value.Year).Where(year => year > 1900).Distinct().OrderByDescending(n => n))
            {
                BEELDBANK_MEDIAOBJECT_FACETS_RESULT result = new BEELDBANK_MEDIAOBJECT_FACETS_RESULT();
                result.results = year;
                yearList.Add(result);
            }
            return yearList;
        }
    }

    public class FacetRepository
    {
        BeeldbankLinqDataContext db = new BeeldbankLinqDataContext();

        public IEnumerable<FacetCollectionModel> getAllFacets(int MediaObjectId)
        {
            return from o in FindAllFacets()
                   where o.MediaObjectId == MediaObjectId
                   select new FacetCollectionModel
                   {
                        RegioFacetId = o.Regio,
                        TechniekFacetId = o.Techniek,
                        RiviergebiedFacetId = o.Riviergebied,
                        ProjectFacetId = o.Project,
                   };
        }


        public void UpdateOrInsert(BEELDBANK_MEDIAOBJECT mediaObject, int id, string Regio, string Techniek, string Riviergebeid, string Project)
        {
            int? RegioSelection = null;
            int? TechniekSelection = null;
            int? RiviergebeidSelection = null;
            int? ProjectSelection = null;

                if (!String.IsNullOrEmpty(Regio))
                {
                    RegioSelection = Convert.ToInt32(Regio);
                }
                if (!String.IsNullOrEmpty(Techniek))
                {
                    TechniekSelection = Convert.ToInt32(Techniek);
                }
                if (!String.IsNullOrEmpty(Riviergebeid))
                {
                    RiviergebeidSelection = Convert.ToInt32(Riviergebeid);
                }
                if (!String.IsNullOrEmpty(Project))
                {
                    ProjectSelection = Convert.ToInt32(Project);
                }

                BEELDBANK_MEDIAOBJECT_FACET o = FindAllFacets().Where(p => p.MediaObjectId == id).FirstOrDefault();
                if (o != null)
                {
                    o.Regio = RegioSelection;
                    o.Techniek = TechniekSelection;
                    o.Riviergebied = RiviergebeidSelection;
                    o.Project = ProjectSelection;
                    db.SubmitChanges();
                }
                else 
                {
                    BEELDBANK_MEDIAOBJECT_FACET p = new BEELDBANK_MEDIAOBJECT_FACET();
                    p.Regio = RegioSelection;
                    p.Techniek = TechniekSelection;
                    p.Riviergebied = RiviergebeidSelection;
                    p.Project = ProjectSelection;
                    mediaObject.BEELDBANK_MEDIAOBJECT_FACETs.Add(p);
                }

        }

        private IQueryable<BEELDBANK_MEDIAOBJECT_FACET> FindAllFacets()
        {
            return db.BEELDBANK_MEDIAOBJECT_FACETs;
        }
        
        public void Save()
        {
            db.SubmitChanges();
        }
    }
    
}